package com.yekong.house.web.controller;

import com.yekong.house.biz.service.*;
import com.yekong.house.common.constants.CommonConstants;
import com.yekong.house.common.constants.HouseUserType;
import com.yekong.house.common.model.*;
import com.yekong.house.common.page.PageData;
import com.yekong.house.common.page.PageParams;
import com.yekong.house.common.vo.ResultMsg;
import com.yekong.house.web.interceptor.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class HouseController {

    @Autowired
    private HouseService houseService;

    @Autowired
    private CityService cityServie;

    @Autowired
    private AgencyService agencyService;

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private CommentService commentService;

    /**
     * 1. 实现分页
     * 2. 支持小区搜索，类型搜索
     * 3. 支持排序
     * 4. 支持展示图片、价格、标题、地址等信息
     *
     * @return
     */

    @RequestMapping("/house/list")
    public String houseList(Integer pageSize, Integer pageNum, House query, ModelMap modelMap){

        PageData<House> ps = houseService.queryHouse(query, PageParams.build(pageSize, pageNum));
        List<House> hotHouses = recommendService.getHotHouse(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses",hotHouses);
        modelMap.put("ps",ps);
        modelMap.put("vo",query);
        return "house/listing";
    }


    /**
     * 1. 查询房屋详情
     * 2. 查询关联经纪人
     * @param id
     * @return
     */
    @RequestMapping("/house/detail")
    public String houseDetail(Long id,ModelMap modelMap){
        House house = houseService.queryOneHouse(id);
        HouseUser houseUser = houseService.getHouseUser(id);
        //热度加1
        recommendService.increase(id);
        List<House> hotHouses = recommendService.getHotHouse(CommonConstants.RECOM_SIZE);
        List<Comment> comments = commentService.getHouseComments(id,8);
        modelMap.put("recomHouses",hotHouses);
        if(houseUser.getUserId() !=null && !houseUser.getUserId().equals(0)){
            modelMap.put("agent",agencyService.getAgentDetail(houseUser.getUserId()));
        }
        modelMap.put("house",house);
        modelMap.put("commentList", comments);
        return "/house/detail";
    }


    /**
     * 1. 房屋留言
     *
     * @param userMsg
     * @return
     */
    @RequestMapping("/house/leaveMsg")
    public String houseMsg(UserMsg userMsg){
        houseService.addUserMsg(userMsg);
        return "redirect:/house/detail?id="+userMsg.getHouseId();
    }


    /**
     * 获取 房产添加页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/house/toAdd")
    public String toAdd(ModelMap modelMap){
        modelMap.put("citys",cityServie.getAllCitys());
        modelMap.put("communitys",houseService.getAllCommunitys());
        return "/house/add";
    }


    /**
     * 添加房产
     * 获取用户
     * 设置房产状态
     * 添加到数据库
     *
     * post
     * @param modelMap
     * @return
     */
    @RequestMapping("/house/add")
    public String doAdd(House house,ModelMap modelMap) {
        User user = UserContext.getUser();
        house.setState(CommonConstants.HOUSE_STATE_UP);
        houseService.addHouse(house,user);
        return "redirect:/house/ownlist";
    }

    /**
     * 个人房产信息
     *
     * @param house
     * @return
     */
    @RequestMapping("/house/ownlist")
    public String ownlist(House house,Integer pageNum,Integer pageSize,ModelMap modelMap){
        User user = UserContext.getUser();
        house.setUserId(user.getId());
        //非收藏
        house.setBookmarked(false);
        modelMap.put("ps",houseService.queryHouse(house,PageParams.build(pageSize,pageNum)));
        modelMap.put("pageType","own");
        return "/house/ownlist";
    }


    // 评分
    @RequestMapping("/house/rating")
    @ResponseBody
    public ResultMsg houseRate(Double rating,Long id){
        houseService.updateRating(id,rating);
        return ResultMsg.successMsg("ok");
    }

    // 2.收藏
    @ResponseBody
    @RequestMapping("/house/bookmark")
    public ResultMsg bookmark(Long id){
        User user = UserContext.getUser();
        houseService.bindUser2House(id, user.getId(), true);
        return ResultMsg.successMsg("ok");
    }

    //3. 删除收藏
    @ResponseBody
    @RequestMapping("/house/unbookmark")
    public ResultMsg unbookmark(Long id){
        User user = UserContext.getUser();
        houseService.unbindUser2House(id,user.getId(),HouseUserType.BOOKMAKR);
        return ResultMsg.successMsg("ok");
    }

    //3. 删除售卖的房产
    @RequestMapping("/house/del")
    public String desale(Long id,String type){
        User user = UserContext.getUser();
        houseService.unbindUser2House(id,user.getId(),type.equals("own")? HouseUserType.SALE : HouseUserType.BOOKMAKR);
        return "redirect:/house/ownlist";
    }

    //4. 收藏列表
    @RequestMapping("/house/bookmarked")
    public String bookmarked(House house,Integer pageNum,Integer pageSize,ModelMap modelMap){
        User user = UserContext.getUser();
        house.setBookmarked(true);
        house.setUserId(user.getId());
        modelMap.put("ps", houseService.queryHouse(house, PageParams.build(pageSize, pageNum)));
        modelMap.put("pageType","book");
        return "/house/ownlist";
    }




}
